import datetime
import HTMLParser
import os
import sys
import urllib
import xbmc
import xbmcgui
import xml.sax.saxutils

class GUI( xbmcgui.WindowXMLDialog ):
	ACTION_EXIT_SCRIPT = ( 9, 10, )

	def __init__( self, *args, **kwargs ):
		# Parse plugin parameters...
		self.params = dict(part.split('=') for part in sys.argv[ 2 ][ 1: ].split( '&' ))
		
		# Prepare parameter values...
		self.type         =                                  self.params[ "type" ]
		self.id           =                                  self.params[ "id"   ]
		self.name         =                  urllib.unquote( self.params[ "name" ] )
		self.version      =                  urllib.unquote( self.params[ "version" ] )
		self.author       =                  urllib.unquote( self.params[ "author" ] )
		self.date         =                  urllib.unquote( self.params[ "date" ] )
		self.date_format  = xbmc.getRegion( "dateshort" ).replace( "MM", "%m" ).replace( "DD", "%d" ).replace( "YYYY", "%Y" ).strip()
		self.date_display = datetime.date  ( int( self.date.split( "/" )[ 2 ] ), int( self.date.split( "/" )[ 0 ] ), int( self.date.split( "/" )[ 1 ] ) ).strftime( self.date_format )
		self.description  = self._html2text( urllib.unquote( self.params[ "description"] ) )
		self.instructions = self._html2text( urllib.unquote( self.params[ "instructions"] ) )
		self.installpath  =                  urllib.unquote( self.params[ "install-path" ] )
		
		# Show dialog window...
		xbmcgui.WindowXML.__init__( self )
		self.doModal()		

	def onInit( self ):
		self.getControl(  10 ).setLabel( self.name )                                                    # name
		self.getControl(  20 ).setLabel( "%s:" % xbmc.getLocalizedString(30301) )                       # version (label)
		self.getControl(  30 ).setLabel( "%s"  % ( self.version ) )  									# version (value)
		self.getControl(  40 ).setLabel( "%s: %s" % ( xbmc.getLocalizedString(30302), self.author ) )	# author
		self.getControl(  50 ).setLabel( "%s:" % xbmc.getLocalizedString(30303) )						# date (label)
		self.getControl(  60 ).setLabel( self.date_display )											# date (value)
		self.getControl(  70 ).setText ( self.description )												# description
		self.getControl(  90 ).setText ( self.instructions )											# instructions
		self.getControl( 110 ).setLabel( xbmc.getLocalizedString(30304) )								# Install
		self.getControl( 120 ).setLabel( xbmc.getLocalizedString(30305) )								# Cancel
		pass

	#
	# Convert HTML to readable strings...
	#
	def _html2text ( self, html ):
		htmlStripper = HTMLStripper()
		htmlStripper.feed( html )
		return htmlStripper.get_fed_data()

	#
	# onClick handler
	#
	def onClick( self, controlId ):
		# Install
		if (controlId == 110) :
			self.close()
			xbmc.executebuiltin( "XBMC.RunPlugin(%s?action=download-install&type=%s&name=%s&id=%s&install-path=%s)" % \
				( sys.argv[ 0 ], self.type, urllib.quote( self.name ), self.id, self.installpath ) )
		# Cancel
		elif (controlId == 120) :
			self.close()

	#
	# onFocus handler
	#
	def onFocus( self, controlId ):
		pass

	#
	# onAction handler
	#
	def onAction( self, action ):
		if ( action in self.ACTION_EXIT_SCRIPT ):
			self.close()


#
# HTMLStripper class - strip HTML tags from text
#
class HTMLStripper (HTMLParser.HTMLParser):
	def __init__(self):
		self.reset()
		self.fed = []
	def handle_data(self, d):
		self.fed.append(d)
	def get_fed_data(self):
		return ''.join(self.fed)
 